package com.xidian.zhiancloud.controller.accidentManage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.constant.AccidentManagementConstant;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.accidentmessage.AccidentMessageQO;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import com.xidian.zhiancloud.util.zyf.ExcelUtils;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentMessageVO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.AccidentMessage;
import com.xidian.zhiancloud.service.accidentManage.AccidentMessageService;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * @ClassName AccidentMessageController
 * @Description 事故基本信息管理
 * @Author zc
 **/
@RestController
@RequestMapping("/accidentMessage")
public class AccidentMessageController extends BaseController {

    @Autowired
    private AccidentMessageService accidentMessageService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrganizationService organizationService;

    /**
     * 添加事故信息
     * @param accidentMessage 传入事故基本信息
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> addAccident(AccidentMessage accidentMessage) {
        accidentMessage.setCompanyId(getSessionUserCompanyId());
        accidentMessage.setCreator(getSessionUserid());
        accidentMessage.setCreateTime(new Date(System.currentTimeMillis()));
        return ResultDTO.successOf(accidentMessageService.addAccidentMessage(accidentMessage),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id查询事故信息
     * @param id 事故id
     * @return  返回事故对象
     */
    @GetMapping("/getById")
    public ResultDTO<AccidentMessageVO> getById(@RequestParam("id") Long id){
        return ResultDTO.successOf(accidentMessageService.getById(id),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 批量删除事故信息
     * @param idList 事故的id数组
     * @return  是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] idList){
        return ResultDTO.successOf(accidentMessageService.deleteByIds(idList),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 更新事故基本信息
     * @param accidentMessage 传入事故对象
     * @return  返回是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(AccidentMessage accidentMessage){
        accidentMessage.setCreator(null);
        accidentMessage.setCompanyId(null);
        accidentMessage.setCreateTime(null);
        accidentMessageService.updateById(accidentMessage);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 条件查询
     * @param accidentMessageQO 请求参数
     * @return 返回事故基本信息列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<AccidentMessageVO>> getListByQO(AccidentMessageQO accidentMessageQO) {
        if(accidentMessageQO.getCurrent() == null || accidentMessageQO.getSize() == null){
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }

        accidentMessageQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(accidentMessageService.getListByQO(accidentMessageQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出
     * @param response
     * @param ids id列表
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,Long[] ids) throws IOException {
        if(ids == null || ids.length==0){
            return;
        }

        List<AccidentMessage> list = accidentMessageService.listByIds(Arrays.asList(ids));
        List<AccidentMessageVO> new_list = new ArrayList<>();

        list.forEach(entry -> {
            AccidentMessageVO vo = new AccidentMessageVO();
            BeanUtils.copyProperties(entry,vo);
            SysUser creator = userService.getById(vo.getCreator());
            vo.setCreatorName(creator == null ? "该用户不存在" : creator.getName());
            Organization org = organizationService.getById(vo.getDepartmentId());
            vo.setDepartmentName(org == null ? "该部门不存在" : org.getName());
            vo.setReviewStatusStr(AccidentManagementConstant.ReviewStatus.get(vo.getReviewStatus()));
            vo.setHandleStatusStr(AccidentManagementConstant.HandleStatus.get(vo.getHandleStatus()));
            new_list.add(vo);
        });

        ExcelUtils.exportExcel(new_list,"事故上报","事故上报",AccidentMessageVO.class,"事故上报",response);
    }
}
